<template>
    <div class="about">
		<!-- <banner isHome="true"></banner> -->
		<div class="focusinfo" style="margin-top: 100px;">
		    <img :src="user.phote">
		</div>
        <div class="site-content">
            <div class="content-warp">
                <div class="about-me about-info">
                    <section-title id="Guestbook"><span>❤</span>我的</section-title>
                    <div class="info-card">
                        <div class="contactForm">
							<ul class="list-group list-group-striped">
								<li class="list-group-item">
									用户名称:{{user.name}}
								</li>
								<li class="list-group-item">
									手机号码:{{user.telephone}}
								</li>
							</ul>
                        </div>
						<el-button @click="alterVO = true;photo=user.phote">修改信息</el-button>
						<el-button @click="phoneVO = true">修改手机号</el-button>
						<el-button @click="passVO = true">修改密码</el-button>
						<!-- 修改弹窗 -->
						<el-dialog title="修改信息" :visible.sync="alterVO" width="500px" append-to-body>
						  图片(点击下方上传头像)：<img :src="photo" style="width: 80px;height: 80px;"/>
						  <el-upload
						  	drag
						  	action="http://localhost:8090/upload/image"
						  	name="multipartFile"
						  	:on-success="uploadImageSuccess"
						  ></el-upload>
						  <tr></tr>
						  用户名称：<el-input v-model="name" />
						  </el-form>
						  <div slot="footer" class="dialog-footer">
						    <el-button type="primary" @click="alterInfo()">确 定</el-button>
						    <el-button @click="cancel">取 消</el-button>
						  </div>
						</el-dialog>
						<!-- 修改手机号弹窗 -->
						<el-dialog title="修改手机号" :visible.sync="phoneVO" width="500px" append-to-body>
						  <tr></tr>
						  用户原手机号：<el-input v-model="telephone" :readonly="true"/>
						  用户新手机号：<el-input v-model="newTelephone" />
						  <button :disabled="isDisable" @click="getcode()">{{ tips }}</button>
						  <el-input v-model="code" placeholder="验证码"/>
						  </el-form>
						  <div slot="footer" class="dialog-footer">
						    <el-button type="primary" @click="alterPhone()">确 定</el-button>
						    <el-button @click="cancel">取 消</el-button>
						  </div>
						</el-dialog>
						<!-- 修改密码弹窗 -->
						<el-dialog title="修改密码" :visible.sync="passVO" width="500px" append-to-body>
						  旧密码：<el-input type="password" v-model="password" show-password />
						  新密码：<el-input type="password" v-model="newPassword" show-password />
						  确认密码：<el-input type="password" v-model="confirmPassword" show-password />
						  </el-form>
						  <div slot="footer" class="dialog-footer">
						    <el-button type="primary" @click="alterPassword()">确 定</el-button>
						    <el-button @click="cancel">取 消</el-button>
						  </div>
						</el-dialog>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
	import sectionTitle from '@/components/section-title'
	import {
		getUserInfo, getCode ,
		alterUserInfo, alterUserPhone,
		alterUserPassword, logout
		} from '@/api/user'
	import { removeToken } from '@/utils/auth'
    export default {
        name: "About",
        data() {
            return {
				passwordType: 'password',
				user: [],
				name: '',
				telephone: '',
				newTelephone: '',
				password: '',
				newPassword: '',
				confirmPassword: '',
				alterVO: false,
				phoneVO: false,
				passVO: false,
				code: '',
				photo: '',
				isDisable: false,
				tips: '获取验证码',
            }
        },
        components: {
			sectionTitle
            // Quote,
        },
        methods: {
			uploadImageSuccess(response, uploadFile, uploadFiles){
				this.photo = response.photo
			},
			async get() {
				let data = await getUserInfo()
				this.user = data.user
				this.name = data.user.name
				this.telephone = data.user.telephone
			},
			// 获取验证码
			async getcode() {
			if (this.newTelephone == '') {
			  alert('输入手机号码不能为空')
			  return
			} else {
			  if (this.isDisable) {
				return
			  } else {
				this.isDisable = true
				let data = await getCode(this.newTelephone)
				alert(data.msg)
				let timer = 120;
				var timeFun = setInterval(() => {
				  --timer;
				  this.tips = timer + "s后重新发送";
				  if (timer == 0) {
					console.log(timer)
					this.tips = "重新发送";
					this.isDisable = false
					clearInterval(timeFun)
				  }
				}, 1000)

			  }
			}
			},
			async alterInfo() {
				await alterUserInfo({
					phote: this.photo,
					name: this.name
				})
				this.cancel()
				this.get()
			},
			async alterPhone() {
				if (this.code == '') {
					alert("未输入验证码")
					return
				}
				await alterUserPhone(this.newTelephone)
				await logout()
				localStorage.removeItem('token')
				removeToken()
				alert("重新登录")
				this.cancel()
				this.$router.push("/login")
			},
			async alterPassword() {
				if (this.newPassword != this.confirmPassword) {
				alert("两次密码不对")
				return
				}
				await alterUserPassword(this.password, this.confirmPassword)
				await logout()
				localStorage.removeItem('token')
				removeToken()
				alert("重新登录")
				this.cancel()
				this.$router.push("/login")
			},
			cancel() {
				this.alterVO = false
				this.phoneVO = false
				this.passVO = false
				this.name = this.user.name
				this.newTelephone =  ''
				this.password = ''
				this.newPassword = ''
				this.confirmPassword =  ''
				this.code = ''
			}
		},
        mounted() {
			this.get()
        }
    }
</script>

<style scoped lang="less">
    .about {
        padding-top: 40px;
    }

	.list-group-striped > .list-group-item {
		border-left: 0;
		border-right: 0;
		border-radius: 0;
		padding-left: 0;
		padding-right: 0;
	}
	.list-group-item {
		border-bottom: 1px solid #e7eaec;
		border-top: 1px solid #e7eaec;
		margin-bottom: -1px;
		padding: 11px 0;
		font-size: 13px;
	}
	.pull-right {
		float: right !important;
	}
	.focusinfo {
	    position: relative;
	    max-width: 800px;
	    padding: 0 10px;
	    top: 40%;
	    left: 50%;
	    transform: translate(-50%,-50%);
	    -webkit-transform: translate(-50%,-50%);
	    text-align: center;
	    img {
			margin-top: 100px;
	        width: 80px;
	        height: 80px;
	        border-radius: 50%;
	        border: 3px solid rgba(255, 255, 255, 0.3);
	    }
	}

    .content-warp {
        margin-top: 80px;

        .about-info {
            margin: 30px 0;

            span {
                color: red;
                margin-right: 10px;
            }

            .info-card {
                min-height: 100px;
                padding: 20px;
                border-radius: 3px;
                margin: 30px 0 50px 0;
                box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
                p{
                    line-height: 1.7rem;
                }
            }
        }
        .contactForm{
			text-align: center;
            width: 100%;
            padding: 20px;
            .form-item{
                align-items: center;
                display: flex;
                &:not(:last-child){
                    margin-bottom: 20px;
                }
                label{
                  width: 80px;
                }
                .v{
                    min-height: 40px;
                    line-height: 20px;
                    border-radius: 3px;
                    padding: 2px 10px;
                    outline:none;
                    border: 1px solid #8fd0cc;
                    width: 100%;
                    resize: vertical;
                }
                button{
                    width: 100px;
                    height: 40px;
                    border-radius: 3px;
                    outline:0;
                    border-style: none;
                    cursor: pointer;
                    background-color: #409eff;
                    color: white;
                    &:hover{
                        opacity: 0.8;
                    }
                }
            }
        }
    }

    /*******/
    @media (max-width: 800px) {
        .content-warp {
            margin-top: 0;
        }
    }
</style>
